<#assign basePath=request.contextPath />

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>tree</title>
	
	<#include '/common/common-css.html'>
	<link rel="stylesheet" href="${basePath}/js/plugins/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
</head>

<body>
	<ul id="tree" class="ztree"></ul>
	<div style="float : right; margin: 10px">
		<button class="btn btn-sm btn-primary">确定</button>
	</div>
	
	<#include '/common/common-js.html'>
	<script type="text/javascript" src="${basePath}/js/plugins/zTree/js/jquery.ztree.all.min.js"></script>
	<script type="text/javascript">
		var roleId = "${id}";
		
		var setting = {
			check: {
				chkboxType:{"Y":"ps","N":"ps"},//勾选checkbox对于父子节点的关联关系  
			    chkStyle:"checkbox",
				enable: true
			},
			data: {
				simpleData: {
					enable: true
				}
			}
		};

		var zNodes =[
			{ id:1, pId:0, name:"会员管理", open:true},
			{ id:11, pId:1, name:"会员管理", open:true},
			{ id:111, pId:11, name:"添加"},
			{ id:112, pId:11, name:"删除"},
			{ id:113, pId:11, name:"修改"},
			{ id:2, pId:0, name:"权限管理",  open:true},
			{ id:21, pId:2, name:"用户管理" , open:true},
			{ id:211, pId:21, name:"添加"},
			{ id:212, pId:21, name:"删除"},
			{ id:313, pId:21, name:"修改"},
			{ id:22, pId:2, name:"角色管理", open:"true"},
			{ id:221, pId:22, name:"添加"},
			{ id:222, pId:22, name:"修改"},
			{ id:223, pId:22, name:"删除"}
		];
		
		
		$(document).ready(function(){
			$.get("${basePath}/permission/dataTree?t=" + Math.random(), function(data){
				var zTree = $.fn.zTree.init($("#tree"), setting, data);
				zTree.expandAll(true);//全部展开  
				
				var nodes = zTree.getNodes();
				var array = zTree.transformToArray(nodes);
				
				var data = {
					"id" : roleId
				}
				
				
				$.ajax({
    				type: 'POST',
    				url: "${basePath}/role/permissionIds",
    				data: { "id" : roleId },
    				dataType: 'json',
    				success: function(treeJson){
    					if(treeJson.error){
    						layer.msg(treeJson.msg,{icon:2,time:1000});
    					}else{
    						for(var i in array){
    							for(var j in treeJson){
    								if(!array[i].isParent){
    									if(array[i].id == treeJson[j]){
    										zTree.checkNode(array[i], true, true);
    									}
    								}
    							}
    						}
    					}
    				},
    				error:function(e) {
    					console.log(e);
    				}
    			});
			});
			
			
			$("button").click(function(){
				var tree = $.fn.zTree.getZTreeObj("tree");
				var nodes = tree.getCheckedNodes();
				var ids = new Array();
				ids.push(roleId);
				for(var i in nodes){
					ids.push(nodes[i].id);
				}
				
				$.ajax({
    				type: 'POST',
    				url: "${basePath}/role/associatePermission",
    				data: JSON.stringify(ids),
    				contentType: "application/json; charset=utf-8",
    				dataType: 'json',
    				success: function(data){
    					if(data.error){
    						layer.msg(data.msg,{icon:2,time:1000});
    					}else{
    						if(data >= 1){
        						layer.msg('关联成功!',{icon:1,time:1000});
        						var index = parent.layer.getFrameIndex(window.name);
        		        		parent.layer.close(index);
        					}
    					}
    				},
    				error:function(e) {
    					console.log(e);
    				}
    			});
			});
		});
	</script>
</body>
</html>